package com.example.demo.controller;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Board;
import com.example.demo.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/board")
@Slf4j
@Api(tags = "板块接口")
public class BoardController {

    // 从配置⽂件中获取主⻚中显⽰的版块个数，默认为9
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Autowired
    private IBoardService boardService;

    @ApiOperation("获取首页板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList()
    {
        log.info("首页板块数量为："+indexBoardNum);
        List<Board> boards=boardService.selectByNum(indexBoardNum);
        if(boards==null)
        {
            boards=new ArrayList<>();
        }

        return AppResult.success(boards);
    }


    @ApiOperation("获取版本信息")
    @GetMapping("getById")
    public AppResult getById(@ApiParam("版本ID") @RequestParam("id")  @NonNull Long id)
    {
        Board board=boardService.selectById(id);
        if(board==null || board.getDeleteState()==1)
        {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        return AppResult.success(board);
    }
}
